import requests
from pyquery import PyQuery as pq


def getText(url):
    kv={'user-agent':'Mozilla/5.0'}
    try:
        r=requests.get(url=url,headers=kv)
        r.raise_for_status()
        print(r.encoding)
        return r.text
    except:
        print('爬取失败！')

def getInfo(html,listInfo):
    doc=pq(html)
    a=doc('a')
    for i in a.items():
        listInfo.append(i.text())

def give(listInfo):
    f=open('collect/lagouwang.txt','w')
    f.close()
    with open('collect/lagouwang.txt','a') as f:
        for i in listInfo:
            f.write(i)
        f.close()

if __name__=='__main__':
    html=getText('https://www.lagou.com/')
    listInfo=[]
    getInfo(html,listInfo)
    give(listInfo)
